<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单位名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入单位名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人姓名" prop="personName">
        <el-input
          v-model="queryParams.personName"
          placeholder="请输入负责人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职务" prop="duty">
        <el-input
          v-model="queryParams.duty"
          placeholder="请输入职务"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进场时间" prop="inTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.inTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择进场时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出差时间" prop="outTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.outTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择出差时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="subcontractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="出差时间" align="center" prop="id" /> -->
      <el-table-column label="单位名称" align="center" prop="name" />
      <el-table-column label="负责人姓名" align="center" prop="personName" />
      <el-table-column label="职务" align="center" prop="duty" />
      <el-table-column label="联系方式" align="center" prop="tel" />
      <el-table-column label="进场时间" align="center" prop="inTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出差时间" align="center" prop="outTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.status === '审核失败'"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.status === '审核失败'"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="agree(scope.row)"
            v-if="roleKey === 'common'&&scope.row.status === '待审核'"
          >同意</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="reject(scope.row)"
            v-if="roleKey === 'common'&&scope.row.status === '待审核'"
          >驳回</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-finished"
            @click="submitAgain(scope.row)"
            v-if="scope.row.status === '审核失败'" 
          >重新提交</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改劳务分包对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单位名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="负责人姓名" prop="personName">
          <el-input v-model="form.personName" placeholder="请输入负责人姓名" />
        </el-form-item>
        <el-form-item label="职务" prop="duty">
          <el-input v-model="form.duty" placeholder="请输入职务" />
        </el-form-item>
        <el-form-item label="联系方式" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="进场时间" prop="inTime">
          <el-date-picker clearable size="small"
            v-model="form.inTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择进场时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出差时间" prop="outTime">
          <el-date-picker clearable size="small"
            v-model="form.outTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择出差时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入多行文本"
          :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}" readonly="true"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="审批意见" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules1" label-width="80px">
        <el-form-item label="备注" >
          <el-input v-model="form.remark" type="textarea" placeholder="请输入多行文本"
          :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="rejectSub">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSubcontract, getSubcontract, delSubcontract, addSubcontract, updateSubcontract, exportSubcontract,agree,submitAgain,reject } from "@/api/system/subcontract";

export default {
  name: "Subcontract",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 劳务分包表格数据
      subcontractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        personName: null,
        duty: null,
        tel: null,
        inTime: null,
        outTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      rules1: {
        remark: [
          { required: true, trigger: "blur", message: "请输入审批意见" }
        ]
      },
      username:"",
      roleKey:""
    };
  },
  created() {
    this.getList();
    this.username = this.$store.state.user.name;
    console.log(this.username);
  },
  methods: {
    reject(row){
      this.form.id = row.id;
      this.open1=true;
    },
    cancel1(){
      this.open1=false;
    },
    agree(row){
      this.form.id = row.id;
      agree(this.form).then(response => {
              this.$modal.msgSuccess("审批成功");
              this.getList();
            });
    },
    rejectSub(){
      reject(this.form).then(response => {
              this.$modal.msgSuccess("审批成功");
              this.open1 = false;
              this.getList();
            });
    },
    submitAgain(row){
      this.form.id = row.id;
      submitAgain(this.form).then(response => {
              this.$modal.msgSuccess("重新发起成功");
              this.getList();
            });
    },
    /** 查询劳务分包列表 */
    getList() {
      this.loading = true;
      listSubcontract(this.queryParams).then(response => {
        this.subcontractList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.roleKey = response.rows[0].roleKey
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        personName: null,
        duty: null,
        tel: null,
        inTime: null,
        outTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加劳务分包";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSubcontract(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改劳务分包";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSubcontract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSubcontract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除劳务分包编号为"' + ids + '"的数据项？').then(function() {
        return delSubcontract(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有劳务分包数据项？').then(() => {
        this.exportLoading = true;
        return exportSubcontract(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
